package com.cmnit.analysis.service

import com.cmnit.analysis.dao.GantryLicencePlateDao
import com.cmnit.analysis.util.{ConfigurationManager, EnvUtil, ServiceUtil}
import org.apache.log4j.Logger

class GantryLicencePlateService {
  private val logger: Logger = Logger.getLogger(this.getClass)
  private val dao = new GantryLicencePlateDao

  def service(time: String): Unit = {
    // 获取加工账期（默认当前时间的前一天）
    val sqlTime = ServiceUtil.getAcctInfo(time)
    val acctTime = ServiceUtil.transformationTimeFormat(time)
    logger.info("加工账期时间：" + acctTime)

    // 注册UDF函数
    EnvUtil.take().udf.register("getVlp", (vehicleplate: String) => ServiceUtil.getVlpUDF(vehicleplate))
    EnvUtil.take().udf.register("getVlpc", (vehicleplate: String) => ServiceUtil.getVlpcUDF(vehicleplate))
    logger.info("注册UDF函数")

    // 创建来源数据视图（门架牌识）
    dao.gantryVehdisData(sqlTime).createOrReplaceTempView("temp_gantryvehdisdata")

    // 生成门架牌识宽表
    dao.gantryVehdisDataDetail().createOrReplaceTempView("temp_gantryvehdisdatadetail")

    // 删除需要插入的账期数据
    dao.delete(ConfigurationManager.getProperty("gantrylicenceplate.table"), ConfigurationManager.getProperty("gantrylicenceplate.time"), acctTime)

    // 写入数据
    dao.write(ConfigurationManager.getProperty("gantrylicenceplate.table"), dao.provinceGantryVehdisData(acctTime))

    // 查看写入结果
    dao.read(ConfigurationManager.getProperty("gantrylicenceplate.table"), ConfigurationManager.getProperty("gantrylicenceplate.time"), acctTime)
  }
}